package com.dukla.picbak.handler;

import com.dukla.picbak.vo.MediaFileInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * 中间数据容器
 */
@Component
public class DataContainer {

    private final Logger logger =
            LoggerFactory.getLogger(DataContainer.class);

    @Value("${pic-backup.data-queue-max}")
    int dataQueueMax;

    private BlockingQueue<MediaFileInfo> picFileQueue;

    @PostConstruct
    private void init(){
        this.picFileQueue=new LinkedBlockingQueue<>(dataQueueMax);
    }

    public void putFileContentQueue(MediaFileInfo dataFileInfoVo){
        try {
            this.picFileQueue.put(dataFileInfoVo);
        } catch (InterruptedException e) {
            logger.error(e.getMessage(),e);
        }
    }

    public MediaFileInfo takeFileContentQueue() throws InterruptedException {
        return this.picFileQueue.take();
    }

    /**
     * 输出运行指标
     */
    @Scheduled(fixedDelayString = "${pic-backup.metric-print-interval}")
    private void printMetric(){
        logger.info("Data Queue Metric: {}/{}",picFileQueue.size(),dataQueueMax);
    }

}
